package com.abbas.abchat.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ListView;

import com.abbas.abchat.AbChatApplication;
import com.abbas.abchat.R;
import com.abbas.abchat.activity.ChatActivity;
import com.abbas.abchat.adapter.ChatAdapter;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.dao.ChatMsgDao;
import com.abbas.abchat.dao.ChatTableDao;
import com.abbas.abchat.dao.impl.AccountDaoImpl;
import com.abbas.abchat.dao.impl.ChatMsgDaoImpl;
import com.abbas.abchat.dao.impl.ChatTableDaoImpl;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatMsg;
import com.abbas.abchat.entity.ChatTable;
import com.abbas.abchat.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ChatFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ChatFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View mView;

    //TODO:
    private ChatTableDao chatTableDao;
    private AccountDao accountDao;
    private ListView lvChat;

    private List<ChatTable> all;
    private List<Account> accounts;

    private ChatAdapter chatAdapter;

    public ChatFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment chatFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ChatFragment newInstance(String param1, String param2) {
        ChatFragment fragment = new ChatFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (mView != null){
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (parent != null){
                parent.removeView(mView);
            }
            return mView;
        }
        mView = inflater.inflate(R.layout.fragment_chat, container, false);
        //TODO you code
        lvChat = (ListView) mView.findViewById(R.id.lv_chat);

        chatTableDao = new ChatTableDaoImpl(getActivity(), AbChatApplication.getAccount().getChatTableDbName());
        accountDao = new AccountDaoImpl(getActivity());

        initData();
        initEvent();

        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData(){

        /**
         * 未完成
         */
//        try {
//            List<Account> accounts1 = accountDao.findListById(new Integer[]{1, 2, 3});
//            Account account = accounts1.get(0);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        try {
            all = chatTableDao.findAll();
            if (all != null && all.size() > 0){
                accounts = accountDao.findListById(all);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (chatAdapter == null){
            chatAdapter = new ChatAdapter(getActivity(),accounts,all);
            lvChat.setAdapter(chatAdapter);
        }else {
            chatAdapter.setAccounts(accounts);
            chatAdapter.setChatTables(all);
        }
    }

    private void initEvent(){
        lvChat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ChatTable chatTable = all.get(position);
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                intent.putExtra("account",accounts.get(position));
                intent.putExtra("dbName",chatTable.getChatName());
                startActivity(intent);
            }
        });
    }


}